package com.workflow.server.service.impl;

import cn.hutool.core.util.StrUtil;
import com.workflow.server.dto.json.ChildNode;
import com.workflow.server.dto.json.Properties;
import com.workflow.server.service.IFlowNodeCreateHandler;
import com.workflow.server.service.IFlowNodeCreateHandlerContext;
import org.camunda.bpm.model.bpmn.builder.AbstractFlowNodeBuilder;
import org.camunda.bpm.model.bpmn.builder.ExclusiveGatewayBuilder;
import org.camunda.bpm.model.bpmn.builder.UserTaskBuilder;
import org.camunda.bpm.model.bpmn.instance.FlowNode;
import org.camunda.bpm.model.bpmn.instance.Process;
import org.camunda.bpm.model.xml.ModelInstance;
import org.camunda.bpm.model.xml.instance.ModelElementInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.workflow.server.enums.FlowNodeType.*;

/**
 * <p>title:发起人节点处理器</p>
 *
 * @author ForeverKobe
 */
@Component
public class ConditionsNodeCreateHandler implements IFlowNodeCreateHandler {
    @Autowired
    IFlowNodeCreateHandlerContext flowNodeCreateHandlerContext;

    @Override
    public FlowNode doCreateNode(String parentNodeId, ChildNode childNode, Process process, Map<String, ChildNode> childNodeMap) {
        childNodeMap.put(childNode.getId(), childNode);
        ModelInstance modelInstance = process.getModelInstance();
        // 获取父节点,发起人节点的父节点是开始节点
        FlowNode parentNode = modelInstance.getModelElementById(parentNodeId);
        String gatewayId = "ExclusiveGateway-" + childNode.getId();
        //先创建开始网关
        ExclusiveGatewayBuilder builder = parentNode.builder()
                .exclusiveGateway()
                .id(gatewayId)
                .name(childNode.getName());

        ChildNode children = childNode.getChildren();
        //如果此处为终点，则返回网关节点
        if (children == null || EMPTY.getType().equals(children.getType()) || children.getId() == null) {
            return builder.getElement();
        }
        //获取各个条件分支
        List<ChildNode> branches = childNode.getBranchs();
        List<String> incoming = new ArrayList<>(branches.size());

        for (ChildNode branch : branches) {
            childNodeMap.put(branch.getId(), branch);

            ChildNode branchChildren = branch.getChildren();
            if (branchChildren == null || EMPTY.getType().equals(branchChildren.getType()) || branchChildren.getId() == null) {

                //如果此处为终点，则返回网关节点
                incoming.add(gatewayId);
                continue;
            }


            FlowNode flowNode = flowNodeCreateHandlerContext.createNode(gatewayId, branchChildren, process, childNodeMap);
            if (flowNode != null){
                incoming.add(flowNode.getId());
            }

        }
        ChildNode childrenNode = childNode.getChildren();
        if (childrenNode == null || EMPTY.getType().equals(childrenNode.getType()) || childrenNode.getId() == null){
            //todo:下一节点为空
        }

        if (StrUtil.isNotBlank(childrenNode.getId())){
            String type = childrenNode.getType();
            if (EMPTY.getType().equals(type)){

            }
        }


        //继续递归后面的节点
        return flowNodeCreateHandlerContext.createNode(gatewayId, children, process, childNodeMap);
    }

    @Override
    public String getType() {
        return CONDITIONS.getType();
    }
}
